<?php

$taskName = $_POST['task_name'];
$taskContent = $_POST['task_content'];

$dsn = "sqlsrv:Server=localhost;Database=Task";
$db = new PDO($dsn, "sa", "123456");

// 增加一个判断，判断任务名称是否已经存在。
$sql = "SELECT * FROM Task where TaskName='".$taskName."' ";
$result = $db->query($sql);
$taskInfo = $result->fetch(PDO::FETCH_ASSOC);
if ($taskInfo) {
    echo "任务已经存在。";
    echo "<a href='task_list.php'>返回列表页面</a>";
    exit();
}

$sql = "INSERT INTO [dbo].[Task]
           ([TaskName]
           ,[TaskContent])
     VALUES
           ('".$taskName."'
           ,'".$taskContent."')";

$result = $db->exec($sql);
if ($result) {
    echo "增加成功<br />";
    echo "<a href='task_list.php'>返回列表页面</a>";
} else{
    echo "增加失败，请联系管理员：上帝打手 邮箱：303410541@qq.com";
    echo "<a href='task_list.php'>返回列表页面</a>";
}